package fw.asmx.launcher.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

public class TileView extends TextView
{
    public static final int TILE_SIZE_SMALL = 1;
    public static final int TILE_SIZE_NORMAL = 2;
    public static final int TILE_SIZE_WIDE = 3;
    public static final int TILE_SIZE_BIG = 4;

    private int tileSize = TILE_SIZE_NORMAL;

    private Drawable appIcon = null;
    private String appName = "";
    private String appPkgName = "";
    private Drawable bk = null;

    public TileView(Context context)
    {
        super(context);
    }

    public TileView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public TileView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    public void initView(Drawable appIcon, String appName, String appPkgName, int tileSize)
    {
        this.appIcon = appIcon;
        this.appName = appName;
        this.appPkgName = appPkgName;
        this.tileSize = tileSize;
        if (tileSize == TILE_SIZE_SMALL)
        {
            appIcon.setBounds(0, 0, 100, 100);
            setText("");
        }
        else
        {
            appIcon.setBounds(0, 0, 200, 200);
            setText(appName);
        }
        setCompoundDrawables(null, appIcon, null, null);
        setBackgroundColor(Color.parseColor("#FF5FA6FF"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
        int spacing = ((TileLayout) getParent()).getTileSpacing();
        int w;
        int h;
        switch (tileSize)
        {
            case TILE_SIZE_SMALL:
                w = (layoutWidth - 3 * spacing) / 4;
                h = w;
                setMeasuredDimension(w, h);
                break;
            case TILE_SIZE_NORMAL:
                w = (layoutWidth - spacing) / 2;
                h = w;
                setMeasuredDimension(w, h);
                break;
            case TILE_SIZE_WIDE:
                w = layoutWidth;
                h = (layoutWidth - spacing) / 2;
                setMeasuredDimension(w, h);
                break;
            case TILE_SIZE_BIG:
                w = layoutWidth;
                h = w;
                setMeasuredDimension(w, h);
                break;
        }
    }

    public int getTileSize()
    {
        return tileSize;
    }

    public void setTileSize(int tileSize)
    {
        this.tileSize = tileSize;
    }

    public Drawable getAppIcon()
    {
        return appIcon;
    }

    public void setAppIcon(Drawable appIcon)
    {
        this.appIcon = appIcon;
    }

    public String getAppName()
    {
        return appName;
    }

    public void setAppName(String appName)
    {
        this.appName = appName;
    }

    public String getAppPkgName()
    {
        return appPkgName;
    }

    public void setAppPkgName(String appPkgName)
    {
        this.appPkgName = appPkgName;
    }
}
